package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.ArticleDao;
import com.guochenglang.www.dao.BaseDao;
import com.guochenglang.www.dao.MemberDao;
import com.guochenglang.www.pojo.Article;
import com.guochenglang.www.pojo.Base;
import com.guochenglang.www.pojo.Member;
import com.guochenglang.www.service.BaseService;
import com.guochenglang.www.service.MemberService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.GetTable;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.lru.MyCapacity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识库业务层
 *
 * @author GuoChenglang
 */
public class BaseServiceImpl implements BaseService {
    private static final BaseDao BASE_DAO = MyDependency.returnObject(BaseDao.class);
    private static final MemberDao MEMBER_DAO = MyDependency.returnObject(MemberDao.class);
    private static final ArticleDao ARTICLE_DAO = MyDependency.returnObject(ArticleDao.class);

    /**
     * 创建新的知识库
     *
     * @param newBase 新的知识库对象
     * @return 主键
     */
    @Affair
    @Override
    public int createBase(Base newBase) throws SQLException, InterruptedException, IllegalAccessException {
        //添加知识库并获取主键
        int id = BASE_DAO.insertBase(newBase);
        //添加成员
        Member comfort = new Member();
        comfort.setBaseId(id);
        comfort.setMemberStatus("1");
        comfort.setMemberIde(newBase.getCreaterId());
        return MEMBER_DAO.insertMember(comfort);
    }

    /**
     * 修改知识库信息
     *
     * @param set        修改的内容
     * @param conditions 修改的条件
     * @return 修改行数
     */
    @Affair
    @Override
    public int changeBase(Base set, Base conditions) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        String key = MyCapacity.getKey(conditions.getId(), GetTable.getTable(Base.class));
        //更新
        int num = BASE_DAO.updateBase(set, conditions);
        //加入缓存或修改缓存值
        MyCapacity.put(key, this.getBase(conditions.getId()));
        return num;
    }

    @Affair
    @Override
    public int deleteBase(Integer id) throws SQLException, InterruptedException, IllegalAccessException {
        Base condition = new Base();
        condition.setId(id);
        BASE_DAO.deleteBase(condition);
        Member conditions = new Member();
        conditions.setBaseId(id);
        //删除有关成员
        MEMBER_DAO.deleteMember(conditions);
        //删除有关文章
        Article articleCondition = new Article();
        articleCondition.setBaseId(id);
        return ARTICLE_DAO.deleteArticle(articleCondition);

    }

    /**
     * 获得具体知识库
     *
     * @param baseId 知识库id
     * @return 具体知识库
     */
    @Override
    public Base getBase(Integer baseId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获取键
        String key = MyCapacity.getKey(baseId, GetTable.getTable(Base.class));
        //从缓存获取
        Base base = (Base) MyCapacity.get(key);
        //缓存不存在
        if (base == null) {
            Base condition = new Base();
            condition.setId(baseId);
            ArrayList<Base> bases = BASE_DAO.baseSelect(condition);
            if (!bases.isEmpty()) {
                base = bases.get(0);
                //加入缓存
                MyCapacity.put(key, base);
            }
        }

        return base;

    }

    /**
     * 获得用户可操纵知识库列表
     *
     * @param userId 用户id
     * @return 知识库列表
     */
    @Override
    public ArrayList<Base> myControlBase(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //创建者的成员表
        ArrayList<Member> members = MEMBER_DAO.memberSelect(new Member(null, null, "1", userId));
        //管理员的成员表
        ArrayList<Member> members2 = MEMBER_DAO.memberSelect(new Member(null, null, "2", userId));
        members.addAll(members2);
        //获得所有知识库id
        List<Integer> baseIds = new ArrayList<>();
        for (Member member : members) {
            baseIds.add(member.getBaseId());
        }
        ArrayList<Base> bases = new ArrayList<>();
        //根据id返回知识库
        for (Integer baseId : baseIds) {
            Base base = new Base();
            base.setId(baseId);
            bases.addAll(BASE_DAO.baseSelect(base));
        }
        return bases;
    }

    /**
     * 获得所有公开的知识库
     *
     * @return 公开的知识库列表
     */
    @Override
    public ArrayList<Base> baseSelectPublic() throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        Base condition = new Base();
        condition.setIsPublic(1);
        return BASE_DAO.baseSelect(condition);
    }

    /**
     * 获得用户加入的知识库
     *
     * @param userId 用户id
     * @return 知识库列表
     */
    @Override
    public ArrayList<Base> myEnterBase(int userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得关于用户的成员表
        Member myMember = new Member();
        myMember.setMemberIde(userId);
        ArrayList<Member> members = MyDependency.returnObject(MemberService.class).baseMemberList(myMember);
        //获得所有的知识库id
        List<Integer> baseIds = new ArrayList<>();
        for (Member member : members) {
            baseIds.add(member.getBaseId());
        }
        ArrayList<Base> bases = new ArrayList<>();
        //根据id获得知识库
        for (Integer baseId : baseIds) {
            Base base = new Base();
            base.setId(baseId);
            ArrayList<Base> bases1 = BASE_DAO.baseSelect(base);
            if (!bases1.isEmpty()) {
                bases.add(bases1.get(0));
            }
        }
        return bases;
    }

}
